describe("EppTime", function() {

    describe("minus", function() {
        it("should be able to subtract 2 times", function() {
            var t1 = EPPTime.fromString('12:00')
            var t2 = EPPTime.fromString('10:00')

            var result = t1.minus(t2);
            expect(result.toString()).toEqual('02:00');

            result = EPPTime.fromString('12:11').minus(EPPTime.fromString('10:22'));
            expect(result.toString()).toEqual('01:49');

            result = EPPTime.fromString('12:10').minus(EPPTime.fromString('13:11'));
            expect(result.toString()).toEqual('-01:01');
        });
    });

    describe("plus", function() {
        it("should be able to add 2 times", function() {
            var result = EPPTime.fromString('12:00').plus(EPPTime.fromString('10:00'))
            expect(result.toString()).toEqual('22:00');

            result = EPPTime.fromString('12:00').plus(EPPTime.fromString('10:00'))
            expect(result.toString()).toEqual('22:00');

            result = EPPTime.fromString('-00:10').plus(EPPTime.fromString('01:00'));
            expect(result.toString()).toEqual('00:50');
        });
    });

    describe("now", function() {
        it("should return a valid time", function() {
            var now = EPPTime.now();
            expect(EPPTime.isTime(now.toString())).toBeTruthy();
        });
    });

    describe("fromString", function() {
        it("should throw exception on invalid times", function() {
            expect(function(){EPPTime.fromString('1000')}).toThrow();
            expect(function(){EPPTime.fromString('aa:bb')}).toThrow();
            expect(function(){EPPTime.fromString('10:00')}).not.toThrow();
            expect(function(){EPPTime.fromString('1:2')}).not.toThrow();
            expect(function(){EPPTime.fromString('134:245')}).not.toThrow();
        });
    });

    describe("calculaHorasTrabalhadas", function() {
        it("deveria calcular horas trabalhadas", function() {
            var ht = EPPTime.calculaHorasTrabalhadas(['10:00', '11:30', '12:45', '14:15']);
            expect(ht.toString()).toEqual('03:00');
        });

        it("deveria ignorar entradas sem saída", function() {
            var ht = EPPTime.calculaHorasTrabalhadas(['10:00', '11:30', '12:45']);
            expect(ht.toString()).toEqual('01:30');
        });

        it("deveria ignorar intervalos negativos", function() {
            var ht = EPPTime.calculaHorasTrabalhadas(['10:00', '11:30', '12:45', '12:15']);
            expect(ht.toString()).toEqual('01:30');
        });

    });

    describe("calculaHorasTrabalhadas com now mockado", function() {
        var now;

        beforeEach(function() {
            now = EPPTime.now;
            EPPTime.now = function() { return new EPPTime(11, 30); };
            spyOn(console, 'log');
        });

        afterEach(function() {
            EPPTime.now = now;
        });

        it("deve usar hora corrente se saida for invalida", function() {
            var ht = EPPTime.calculaHorasTrabalhadas(['10:00', '-']);
            expect(ht.toString()).toEqual('01:30');
            expect(console.log).not.toHaveBeenCalled();
        });

        it("deve ignorar entrada se saida for invalida e consideraHoraCorrente for false", function() {
            var ht = EPPTime.calculaHorasTrabalhadas(['10:00', '-', '11:00', '12:00'], false);
            expect(ht.toString()).toEqual('01:00');
            expect(console.log).toHaveBeenCalled();
        });

        it("não deve alterar array", function() {
            var array = ['10:00', '11:00'];
            var ht = EPPTime.calculaHorasTrabalhadas(array, false);
            expect(array.length).toEqual(2);
        });
    });

});

describe("EPPDate", function() {

    describe("toStringDMY", function() {

        it("deveria converter para string corretamente", function() {
            var stringDMY = new Date(2014, 08, 19).toStringDMY();
            expect(stringDMY).toEqual('19/09/2014');
        });

    });

    describe("ehHoje", function() {

        it("deveria reconhecer o dia de hoje", function() {
            var ehHoje = EPPDate.ehHoje(new Date().toStringDMY());
            expect(ehHoje).toBeTruthy();
        });

    });

});
